package com.example.myapplication.activity;

import android.animation.ValueAnimator;
import android.os.Message;
import android.util.Log;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.myapplication.R;

/**
 * 智慧烟感控制界面
 */
public class SmokeActivity extends BaseActivity {

    @Override
    protected void doHttpData(Message message) {
        switch (message.what) {
            case 0:
                //显示属性
                String string = (String) message.obj;
                Log.d("zoubo", string);
                String response = JSON.parseObject(string).getString("response");
                JSONObject properties =
                        JSON.parseObject(response).getJSONArray("services").getJSONObject(0).getJSONObject(
                                "properties");
                Log.d("zoubo", properties.toJSONString());
                //设置属性
                String smoke_value = properties.getString("Smoke_Value");
                ValueAnimator valueAnimator = ValueAnimator.ofFloat(0.0f, Float.valueOf(smoke_value)).setDuration(1000);
                valueAnimator.setDuration(1000);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        value.setText(animation.getAnimatedValue().toString());
                    }
                });
                valueAnimator.start();
                //设置状态
                status.setText(properties.getString("BeepStatus").equals("OFF") ? "关闭" : "开启");
                //开启定时轮询（查询设备属性）
                if (myScheduleTask == null) {
                    myScheduleTask = new MyScheduleTask();
                }
                getMyHttpHandler().postDelayed(myScheduleTask, 2000);
                break;
            case 1:
                String string2 = (String) message.obj;
                Log.d("zoubo", "下发命令响应:" + string2);
                String result_code = JSON.parseObject(string2).getJSONObject("response").getString("result_code");
                Log.d("zoubo", result_code);
                if (result_code.equals("0")) {
                    control.setEnabled(true);
                    //同步状态
                    status.setText(control.isChecked() ? "开启" : "关闭");
                } else {
                    Toast.makeText(SmokeActivity.this, "设备响应失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    protected String serviceID() {
        return "Smoke";
    }

    @Override
    protected int layoutID() {
        return R.layout.activity_content;
    }

    private TextView value;
    private TextView status;
    private Switch control;

    @Override
    protected void initView() {
        value = (TextView) findViewById(R.id.value);
        status = (TextView) findViewById(R.id.status);
        control = (Switch) findViewById(R.id.control);
        //设备控制监听器
        MyControlListener myControlListener = new MyControlListener("Smoke_Control_Beep", "Beep", 1);
        control.setOnClickListener(myControlListener);
    }
}
